<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/30
 * Time: 22:47
 */

namespace app\company\controller;


use app\common\model\BasicArea;
use app\common\model\SalesCompany;
use think\Exception;

class Company extends Admin
{
    /**
     * 获取公司列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCompanyList(){
        $user_company_id = input('user_company_id',0);
        $companyModel = new SalesCompany();
        $list = $companyModel->select();
        $info = $this->tree_menu($list,$user_company_id);
        $res['status'] = [
            'code'=>200,
            'message'=>'success'
        ];
        if($user_company_id){
            $resInfo = $companyModel->field('id,company_name as title,parent_id as parentId')->where(['id'=>$user_company_id])->select();
            if(!empty($self)){
                $resInfo[0]['children'] = $info;
            }
        }else{
            $resInfo = $info;
        }

        $res['data'] = $resInfo;
        echo json_encode($res);
        die;
    }

    /**
     * 组织菜单树形结构
     */
    private function tree_menu($menu_arr,$parent_id){
        $totall_arr = array();
        foreach ($menu_arr as $temp){
            if($parent_id == $temp['parent_id']){
                $menu_data = [];
                $menu_data['id'] = $temp['id'];
                $menu_data['title'] = $temp['company_name'];
                $menu_data['parentId'] = $temp['parent_id'];
                $menu_data["children"] = $this->tree_menu($menu_arr,$temp['id']);
                $totall_arr[] = $menu_data;
            }
        }
        return $totall_arr;
    }

    /**
     * 获取公司信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getInfo(){
        $id = input('id');
        $showCity = input('showCity',0);
        $model = new SalesCompany();
        $info = $model->with('parent')->where(['id'=>$id])->find();
        if($info){
            $info['detail_address'] = $info['province'].$info['city'].$info['district'].$info['address'];
            if($showCity){
                $info['city_list'] = $this->getAreaList($info['city']);
                $info['district_list'] = $this->getAreaList($info['district']);
            }
        }
        json_return_code($info);
    }

    public function getAreaList($name){
        $info = BasicArea::where('name', $name)->find();
        if($info){
            $list = BasicArea::where('parent_id', $info['parent_id'])->select();
            return $list;
        }
        return [];
    }
    /**
     * 新增修改公司
     * @return \think\response\Json
     */
    public function modifyCompany(){
        $id = input('id');
        $data['parent_id'] = input('parent_id',0,'trim');
        $data['company_name'] = input('company_name','','trim');
        $data['province'] = input('province','','trim');
        $data['city'] = input('city','','trim');
        $data['district'] = input('district','','trim');
        $data['address'] = input('address','','trim');
        $data['user_name'] = input('user_name','','trim');
        $data['phone'] = input('phone','','trim');
        $data['common'] = input('common','','trim');

        $model = new SalesCompany();
        if($id){
            $data['id'] = $id;
            $result = $this->validate($data,'Company.save');
            if ($result !== true) json_return_code('',$result,201);
            $res = $model->isUpdate(true)->save($data,array('id'=>$id));
            $msg = '修改';
            $company_id = $id;
        }else{
            $data['add_datetime'] = date('Y-m-d H:i:s');
            $result = $this->validate($data,'Company.add');
            if ($result !== true) json_return_code('',$result,201);
            $res = $model->insertGetId($data);
            $msg = '添加';
            $company_id = $res;
        }
        if($res !== false){
            $info = $model->where('id',$company_id)->find();
            json_return_code($info,$msg.'成功');
        }else{
            json_return_code('',$msg.'失败',201);
        }
    }

    /**
     * 删除
     */
    public function del(){
        $id = input('id');
        $model = new SalesCompany();
        //1 参数验证是否合法
        if (empty($id)){
            json_return_code('','请选择公司',201);
        }
        $con['parent_id'] = $id;
        $childCount = $model->where($con)->count();
        if($childCount > 0){
            json_return_code('','有子公司，不能删除',201);
        }
        $model::destroy($id);
        json_return_code('删除成功');
    }
}